package cn.itcast.consumer.web;

import cn.itcast.consumer.client.UserClient;
import cn.itcast.consumer.pojo.User;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController   //返回值是json
@RequestMapping("consumer")   //添加一级目录
//@DefaultProperties(defaultFallback = "queryUserByIdFallBack1")   //降级逻辑,在类中所有方法生效
public class ConsumerController {

    @Autowired
     private UserClient userClient;

    /**
     * Feign远程代理
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id") Long id) {
        User user = userClient.queryById(id);
        return user;
    }

    /*@Autowired   //自动注入
    private RestTemplate restTemplate;*/

    /**
     * 负载均衡+熔断器
     * 降级逻辑在类中所有方法生效
     * 熔断
     * @param id
     * @return
     */
    /*@GetMapping("{id}")
    @HystrixCommand(commandProperties = {
            //触发熔断的最小请求次数，默认20
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "20"),
            //休眠时长，默认是5000毫秒
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
            //触发熔断的失败请求最小占比，默认50%
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
    })    //降级逻辑
    public String queryUserById(@PathVariable("id") Long id) {
        if (1 == id) {
            throw new RuntimeException("太忙了");
        }
        String url = String.format("http://user-service/user/%d", id);
        String user = restTemplate.getForObject(url, String.class);
        return user;
    }*/

    /**
     * 降级逻辑方法的具体实现
     * @param
     * @return
     */
    /*public String queryUserByIdFallBack1() {
        return "服务器忙,请稍后重试!";
    }*/


    /**
     * 负载均衡+熔断器
     * 降级逻辑只在这个方法上生效
     * @param id
     * @return
     */
    /*@GetMapping("{id}")
    @HystrixCommand(fallbackMethod = "queryUserByIdFallBack")   //降级逻辑,仅在此方法上生效
    public String queryUserById(@PathVariable("id") Long id) {
        String url = String.format("http://user-service/user/%d", id);
        String user = restTemplate.getForObject(url, String.class);
        return user;
    }*/

    /**
     * 降级逻辑方法的具体实现
     * @param id
     * @return
     */
    /*public String queryUserByIdFallBack(Long id) {
        return "服务器忙,请稍后重试!";
    }*/


    /**
     * 负载均衡
     * @param id
     * @return
     */
    /*@GetMapping("{id}")
    public User queryUserById(@PathVariable("id") Long id) {
        String url = String.format("http://user-service/user/%d", id);
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }*/

    /*@Autowired
    private DiscoveryClient discoveryClient;*/

    //private int count = 0;

   /* @GetMapping("{id}")
    public User queryUserById(@PathVariable("id") Long id) {
        List<ServiceInstance> instances = discoveryClient.getInstances("user-Service");
        ServiceInstance instance = instances.get(0);
        //count ++;
        //String url = "http://localhost:8081/user/2";
        String url = String.format("http://%s:%d/user/%d", instance.getHost(), instance.getPort(), id);
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }*/
}
